#!/usr/bin/python
#coding=utf-8

import redis
import configparser
import sys

class QueryRedis():
    def __init__(self):
        cp = configparser.ConfigParser()
        the_dir = sys.path[0]
        cp.read(the_dir + '/db.conf')
        host = cp.get('redis', 'host')
        port = cp.get('redis', 'port')
        passwd = cp.get('redis', 'password')
        self.prefix = cp.get('redis', 'prefix')

        self.redis = redis.Redis(host=host, port=port, password=passwd)

    # 设置值
    def set(self, key, value, expire=3600):
        key = self.prefix + key
        self.redis.set(key, value, expire)

    # 获取值
    def get(self, key):
        key = self.prefix + key
        val = self.redis.get(key)
        if val:
            return val
        else:
            return None

    # 删除值
    def delkey(self, key):
        key = self.prefix + key
        self.redis.delete(key)

    # 判断值是否存在
    def exists(self, key):
        key = self.prefix + key
        return self.redis.exists(key)